var attnhistory = {
    events : {
        ".attnhistory-body .search-record-list click" : "search",
        ".attnhistory-body .show-img click" : "showImg",
        ".attnhistory-body .record-export click" : "recordExport",
    },
	init : function(){
		var _this = this;
        document.title = "考勤设置 - 考勤管理";
        var temp = Util.template($("#attnhistory-template").html(),{});
        $(".commonBody-rightBody").html(temp);
        Util.event.call(_this,_this.events);
        $(".date-from").datetimepicker({
            format: 'yyyy-mm-dd',
            autoclose: true,
            language: "zh-CN",
            pickerPosition: "bottom-left",
            minView:2
        });
        this.getDeptList();
	},
    getDeptList : function(){
        var that = this;
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getHolidayDeptList(),
            dataType: "json",
            showLoading : false,
            success : function(data){
                if(data.code == 200){
                    $.each(data.result,function(i,v){
                        $("#depId").append('<option value="'+v.id+'">'+v.name+'</option>');
                    })
                    that.getHistoryList();
                }
            }
        })
    },
    search : function(){
        this.getHistoryList();
    },
	getHistoryList : function(page){
        var obj = Util.getData($(".attnhistory-body")),that = this;
        obj.startDate = obj.startDate != "" ? obj.startDate + " 00:00:00" : "";
        obj.endDate = obj.endDate != "" ? obj.endDate + " 23:59:59" : "";
        obj.currentPage = Util.isEmpty(page)?1:page;
        obj.pageSize = pageInfo.pageSize();
        Util.ajaxRequest({
            type: "post",
            url: interfaceURL.historyRecord(),
            dataType: "json",
            data : obj,
            success : function(data){
                if(data.code == 200){
                    that.showTable(data.result);
                }
            }
        })
    },
    showTable : function(data){
        $.each(data.results,function(i,v){
            v.sex = v.sex == 1 ? "男" : "女";
            v.validateWay = v.validateWay == 1 ?"人脸": v.validateWay == 2 ?  "人证合一" : v.validateWay == 3 ? "身份证" : "指纹";
            v.operation = '<button class="btn btn-blue show-img">查看</button>';
        })
        var that = this;
        $(".history-list").cakeTable({
            head: [
                {field: "idNum",name: "工号"},
                {field: "realName",name: "姓名"},
                {field: "sex",name: "性别"},
                {field: "depName",name: "部门"},
                {field: "validateWay",name: "验证方式"},
                {field: "inTime",name: "时间",format : "time"},
                {field: "operation",name: "操作",type:2}
            ],
            rowData : [{key:"id",attrName:"data-id"}],
            sort: false,
            columns: data.results,
            pageInfo:data.pagination2,
            align:"center",
            pageType:"out",
            page : function(page){
                that.getHistoryList(page);
            },
        })
    },
    showImg : function(ele){
        var id = $(ele).parents("tr").attr('data-id');
        var url = interfaceURL.showImage() + "?recordId=" + id;
        pop.dialog({
            popTitle: "查看图片",
            popContent: '<div class="history-img"><img  id="historyImg" src="'+url+'"></div>',
            width :　"595px",
            maxHeight : "500px",
            popOkText : "关闭",
            popCancelShow : false,
            onLoad : function(){
                $("#historyImg").load(function(){
                    pop.resize();
                });
            }
        })
    },
    recordExport : function(){
        var obj = [];
        obj.push("depId=" +  $("#depId").val());
        obj.push("endDate=" +  ($("#endDate").val() == "" ? "" : $("#endDate").val() + " 23:59:59"));
        obj.push("idNum="  + $("#idNum").val());
        obj.push("realName=" + $("#realName").val());
        obj.push("startDate=" + ($("#startDate").val() == "" ? "" : $("#startDate").val() + " 00:00:00"));
        obj.push("validateWay=" + $("#validateWay").val());
        window.open(interfaceURL.exportRecord() + "?" + obj.join("&"));
    }
};